package cn.itcast.bos.service.take_delivery.impl;

import cn.itcast.bos.dao.take_delivery.WayBillRepository;
import cn.itcast.bos.index.WayBillIndex;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.domain.take_delivery.WayBill;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xbqco on 2018/7/20.
 */
@Service
public class WayBillServiceImpl implements WayBillService{

    @Autowired
    private WayBillRepository wayBillRepository;
    @Autowired
    private WayBillIndex wayBillIndex;
    @Override
    public String save(WayBill wayBill) {
        wayBillRepository.save(wayBill);
        return "success";
    }

    @Override
    public Page<WayBill> findPage(WayBill wayBill,Pageable pageable) {

       if (StringUtils.isBlank(wayBill.getWayBillNum())
                && StringUtils.isBlank(wayBill.getSendAddress())
               && StringUtils.isBlank(wayBill.getRecAddress())
                && StringUtils.isBlank(wayBill.getSendProNum())
              && (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {

               return wayBillRepository.findAll(pageable);
           } else{
               BoolQueryBuilder query = new BoolQueryBuilder();
               if (StringUtils.isNotBlank(wayBill.getWayBillNum())) {
                   QueryBuilder queryBuilder = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
                   query.must(queryBuilder);
               }
               if (StringUtils.isNotBlank(wayBill.getSendAddress())) {
                   QueryBuilder queryBuilder = new TermQueryBuilder("sendAddress", wayBill.getSendAddress());
                   query.must(queryBuilder);
               }
               if (StringUtils.isNotBlank(wayBill.getRecAddress())) {

                   QueryBuilder queryBuilder1 = new WildcardQueryBuilder("recAddress", "*" + wayBill.getRecAddress() + "*");
                   QueryBuilder queryBuilder2 = new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress")
                           .defaultOperator(QueryStringQueryBuilder.Operator.AND);
                   BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                   boolQueryBuilder.should(queryBuilder1);
                   boolQueryBuilder.should(queryBuilder2);
                   query.must(boolQueryBuilder);
               }
               if (StringUtils.isNotBlank(wayBill.getSendProNum())) {
                   QueryBuilder queryBuilder = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
                   query.must(queryBuilder);
               }

               if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
                   QueryBuilder queryBuilder = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
                   query.must(queryBuilder);
               }
               SearchQuery searchQuery = new NativeSearchQuery(query);
               searchQuery.setPageable(pageable);
               return wayBillIndex.search(searchQuery);

           }

       }

    @Override
    @RequiresPermissions("waybill:saveAsXls")
    public List<WayBill> findPage(WayBill wayBill) {

        if (StringUtils.isBlank(wayBill.getWayBillNum())
                && StringUtils.isBlank(wayBill.getSendAddress())
                && StringUtils.isBlank(wayBill.getRecAddress())
                && StringUtils.isBlank(wayBill.getSendProNum())
                && (wayBill.getSignStatus() == null || wayBill.getSignStatus() == 0)) {

            return wayBillRepository.findAll();
        } else{
            BoolQueryBuilder query = new BoolQueryBuilder();
            if (StringUtils.isNotBlank(wayBill.getWayBillNum())) {
                QueryBuilder queryBuilder = new TermQueryBuilder("wayBillNum", wayBill.getWayBillNum());
                query.must(queryBuilder);
            }
            if (StringUtils.isNotBlank(wayBill.getSendAddress())) {
                QueryBuilder queryBuilder = new TermQueryBuilder("sendAddress", wayBill.getSendAddress());
                query.must(queryBuilder);
            }
            if (StringUtils.isNotBlank(wayBill.getRecAddress())) {

                QueryBuilder queryBuilder1 = new WildcardQueryBuilder("recAddress", "*" + wayBill.getRecAddress() + "*");
                QueryBuilder queryBuilder2 = new QueryStringQueryBuilder(wayBill.getRecAddress()).field("recAddress")
                        .defaultOperator(QueryStringQueryBuilder.Operator.AND);
                BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
                boolQueryBuilder.should(queryBuilder1);
                boolQueryBuilder.should(queryBuilder2);
                query.must(boolQueryBuilder);
            }
            if (StringUtils.isNotBlank(wayBill.getSendProNum())) {
                QueryBuilder queryBuilder = new TermQueryBuilder("sendProNum", wayBill.getSendProNum());
                query.must(queryBuilder);
            }

            if (wayBill.getSignStatus() != null && wayBill.getSignStatus() != 0) {
                QueryBuilder queryBuilder = new TermQueryBuilder("signStatus", wayBill.getSignStatus());
                query.must(queryBuilder);
            }
            SearchQuery searchQuery = new NativeSearchQuery(query);

            return wayBillIndex.search(searchQuery).getContent();

        }

    }


    @Override
    public WayBill findByWayBillNUm(String wayBillNum) {
        return wayBillRepository.findWayBillByWayBillNum(wayBillNum);
    }
}
